{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import re\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "def YearQ(start_year='2015',end_year='2020',quarter='q2'):     \n",
    "    date_list = []      \n",
    "    for year in range(int(start_year), int(end_year)+1):     \n",
    "            date_list.append(str(year) + quarter)    \n",
    "    return date_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['2015q2', '2016q2', '2017q2', '2018q2', '2019q2', '2020q2']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "period=YearQ('2015','2020','q2')\n",
    "period"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "def FinancialReport(date_list=period,report='valuation'):\n",
    "    start = time.clock()\n",
    "    rets = [get_fundamentals(query(report),statDate=date) \n",
    "      for date in date_list]\n",
    "\n",
    "    df = pd.DataFrame(rets[0])\n",
    "\n",
    "    for ret in rets[1:]:\n",
    "        df = df.append(pd.DataFrame(ret))\n",
    "    \n",
    "    name = str(report)\n",
    "    pattern = \"<class 'jqdata.fundamentals_tables_gen.(.*)'>\"\n",
    "    filename = re.findall(pattern,name)[0]\n",
    "    \n",
    "    df.to_csv(filename + '_' + date_list[0] + '_' + date_list[-1] + '.csv') \n",
    "    print('{} report from {} to {} is successfully loaded and saved.'.format(filename,date_list[0],date_list[-1]))\n",
    "    end = time.clock()\n",
    "    print('Running time: {} seconds'.format(round(end-start,1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "StockValuation report from 2015q2 to 2020q2 is successfully loaded and saved.\n",
      "Running time: 1.1 seconds\n"
     ]
    }
   ],
   "source": [
    "# 市值数据\n",
    "FinancialReport(report=valuation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BalanceSheet report from 2015q2 to 2020q2 is successfully loaded and saved.\n",
      "Running time: 4.1 seconds\n"
     ]
    }
   ],
   "source": [
    "# 资产负债数据\n",
    "FinancialReport(report=balance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CashFlowStatement report from 2015q2 to 2020q2 is successfully loaded and saved.\n",
      "Running time: 2.8 seconds\n"
     ]
    }
   ],
   "source": [
    "# 现金流数据\n",
    "FinancialReport(report=cash_flow)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IncomeStatement report from 2015q2 to 2020q2 is successfully loaded and saved.\n",
      "Running time: 2.1 seconds\n"
     ]
    }
   ],
   "source": [
    "# 利润数据\n",
    "FinancialReport(report=income)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "FinancialIndicator report from 2015q2 to 2020q2 is successfully loaded and saved.\n",
      "Running time: 1.8 seconds\n"
     ]
    }
   ],
   "source": [
    "# 财务指标常用数据\n",
    "FinancialReport(report=indicator)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "MarkDown菜单",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
